package com.huzz.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.huzz.entity.SysRole;
import com.huzz.model.RoleModel;
import com.huzz.service.ISysRoleService;
import com.huzz.util.body.JsonResult;
import com.huzz.util.body.ResultTool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Name 名称
 * @Description 描述
 * @Author 童年的纸飞机
 * @Since 2023-04-2023/4/27 11:19
 */
@Api(tags = "系统角色模块 API")
@RestController
@RequestMapping("/api/role")
public class SysRoleController {

    @Autowired
    private ISysRoleService roleService;

    @ApiOperation(value = "新建角色")
    @PostMapping("/createRole")
    public JsonResult<String> createRole(@RequestBody @Validated RoleModel roleModel) {
        return ResultTool.response(roleService.createRole(roleModel));
    }

    @ApiOperation(value = "查询角色分页列表")
    @GetMapping("/findRoleListPage")
    public JsonResult<IPage<SysRole>> findRoleListPage(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, RoleModel model) {
        return ResultTool.success(roleService.findRoleListPage(pageNum, pageSize, model));
    }

    @ApiOperation(value = "编辑角色")
    @PutMapping("/updateRoleById")
    public JsonResult<String> updateRoleById(@RequestBody SysRole role) {
        return ResultTool.response(roleService.updateRoleById(role));
    }

    @ApiOperation(value = "删除角色")
    @DeleteMapping("/removeRoleById")
    public JsonResult<String> removeRoleById(@RequestParam Integer id) {
        return ResultTool.response(roleService.removeRoleById(id));
    }

    @ApiOperation(value = "给角色分配权限")
    @PostMapping("/distributePermission")
    public JsonResult<String> distributePermission(@RequestParam Integer roleId,@RequestParam List<Integer> permissionIdList) {
        return ResultTool.response(roleService.distributePermission(roleId, permissionIdList));
    }

    @ApiOperation(value = "给角色分配菜单")
    @PostMapping("/distributeMenu")
    public JsonResult<String> distributeMenu(@RequestParam Integer roleId,@RequestParam List<Integer> menuIdList) {
        return ResultTool.response(roleService.distributeMenu(roleId, menuIdList));
    }

    @ApiOperation(value = "查询绑定到该角色的权限id数组")
    @GetMapping("/findPermissionIdListBeSelected")
    public JsonResult<Integer[]> findPermissionIdListBeSelected(@RequestParam Integer roleId) {
        return ResultTool.success(roleService.findPermissionIdListBeSelected(roleId));
    }

    @ApiOperation(value = "查询绑定到该角色的菜单id数组")
    @GetMapping("/findMenuIdListBeSelected")
    public JsonResult<Integer[]> findMenuIdListBeSelected(@RequestParam Integer roleId) {
        return ResultTool.success(roleService.findMenuIdListBeSelected(roleId));
    }

}
